import 'package:boss/ui/widget/calendar/provider/calendar_provider.dart';
import 'package:boss/ui/widget/calendar/util/date_util.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'calendar_day_cell.dart';

class CalendarView extends StatelessWidget {
  final int index;

  const CalendarView({Key key, this.index}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    var padding = width % 7 / 2 + 14;

    return Padding(
      padding: EdgeInsets.symmetric(
        horizontal: padding,
      ),
      child: Consumer<CalendarProvider>(
        builder: (context, provider, child) {
          var dateTime = DateUtils.addMonthsToMonthDate(provider.minDateTime, index);
          var days = DateUtils.getDays(dateTime, minDateTime: provider.minDateTime);

          List<Widget> children = [];
          days.forEach((current) {
            children.add(CalendarDayCell(
              data: current,
              onTap: () {
                if (!current.isDisabled) {
                  var calendarProvider = Provider.of<CalendarProvider>(
                    context,
                    listen: false,
                  );
                  var dateTime = DateUtils.addMonthsToMonthDate(
                    calendarProvider.minDateTime,
                    calendarProvider.currentPageIndex,
                  );
                  dateTime = DateTime(dateTime.year, dateTime.month, int.parse(current.day));

                  calendarProvider.setDateTime(dateTime);
                }
              },
            ));
          });

          return GridView.custom(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 7,
              mainAxisSpacing: 40.w,
            ),
            padding: EdgeInsets.all(0),
            childrenDelegate: SliverChildListDelegate(
              children,
              addRepaintBoundaries: false,
            ),
          );
        },
      ),
    );
  }
}
